package com.scujj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scujj.mapper.BlogMapper;
import com.scujj.pojo.Blog;
import com.scujj.pojo.CommentBlog;
import com.scujj.pojo.Result;
import com.scujj.pojo.User;
import com.scujj.service.BlogService;
import com.scujj.service.UserService;
import com.scujj.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    BlogMapper mapper;

    @Autowired
    UserService userService;


    @Override
    public void publishBlog(long uid, String context, String imgSrc, Long retransmissionBlogId) {
        Blog blog = new Blog();
        blog.setUid(uid);
        blog.setContext(context);
        blog.setImgSrc(imgSrc);
        blog.setPublishTime(new Date());
        blog.setRetransmissionBlogId(retransmissionBlogId);
        mapper.insert(blog);
    }

    @Override
    public void delBlog(long id) {
        mapper.deleteById(id);
    }

    @Override
    public Blog getBlogById(long id) {
        return mapper.selectById(id);
    }

    @Override
    public List<Blog> lastBlogList(int page, int n) {
        List<Blog> blogs = mapper.getLastBlogList(n, (page - 1) * n);
        return blogListSet(blogs);
    }

    @Override
    public List<Blog> hotBlogList(int page, int n) {
        return blogListSet(mapper.getHotBlogList(n, (page - 1) * n));
    }

    @Override
    public List<Blog> subscribeBlogList(int page, int n, long uid) {
        Object data = userService.getSubscribeList(uid).getData();
        User user = ResultUtil.singleTrans(data, User.class);
        if (user.getSubcribeList() == null) {
            return new LinkedList<>();
        }
        List<User> userList = user.getSubcribeList();
        return blogListSet(mapper.getSubscribeBlogList(n, (page - 1) * n, userList));
    }

    @Override
    public List<Blog> blogListSet(List<Blog> blogs) {
        List<Long> ids = new LinkedList<>();
        blogs.forEach(b -> ids.add(b.getUid()));
        if (ids.isEmpty()) {
            return blogs;
        }
        HashMap<Long, User> userMap = getUserMap(ids);
        blogs.forEach(b -> {
            b.setUser(userMap.get(b.getUid()));
        });
        return commentListSet(thumbsUpListSet(blogs));
    }

    public List<Blog> thumbsUpListSet(List<Blog> blogs) {
        List<Long> ids = new LinkedList<>();
        blogs.forEach(blog -> {
            blog.getThumbsUpUserList().forEach(user -> {
                ids.add(user.getId());
            });
        });
        if (ids.isEmpty()) {
            return blogs;
        }
        HashMap<Long, User> userMap = getUserMap(ids);
        blogs.forEach(blog -> {
            List<User> userList = blog.getThumbsUpUserList();
            userList.forEach(user -> {
                user.setUserName(userMap.get(user.getId()).getUserName());
                user.setHeadImgSrc(userMap.get(user.getId()).getHeadImgSrc());
            });
        });
        return blogs;
    }

    public List<Blog> commentListSet(List<Blog> blogs) {
        List<Long> ids = new LinkedList<>();
        blogs.forEach(blog -> {
            blog.getCommentBlogList().forEach(comment -> {
                ids.add(comment.getUid());
            });
        });
        if (ids.isEmpty()) {
            return blogs;
        }
        HashMap<Long, User> userMap = getUserMap(ids);
        blogs.forEach(blog -> {
            List<CommentBlog> commentList = blog.getCommentBlogList();
            commentList.forEach(comment -> {
                comment.setUser(userMap.get(comment.getUid()));
            });
        });
        return blogs;
    }

    public HashMap<Long, User> getUserMap(List<Long> ids) {
        Result r = userService.getUserByIdList(ids);
        List<User> users = ResultUtil.listTrans(r.getData(), User.class);
        HashMap<Long, User> userMap = new HashMap<>();
        users.forEach(u -> {
            userMap.put(u.getId(), u.setPassword(null).setPhoneNumber(null).setMoney(0L));
        });
        return userMap;
    }

}
